package cn.lingyangwl.agile.pay.common.config;

import cn.lingyangwl.agile.pay.common.constants.MqCons;
import cn.lingyangwl.agile.pay.common.model.mq.PayOrderCloseMQ;
import cn.lingyangwl.agile.pay.common.model.mq.MchNotifyMQ;
import cn.lingyangwl.agile.pay.common.model.mq.RefundOrderMQ;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * 延时消息队列
 *
 * @author shenguangyang
 */
@Configuration
public class PayMqDelayedConfig {
    @PostConstruct
    public void init() {
        System.out.println("pay order mq binding success!!");
    }

    // ============================================================= 订单延时队列绑定配置

    /**
     * 自定义交换机 我们在这里定义的是一个延迟交换机
     * 需要在rabbitmq上安装延时插件
     * 在官网上下载 <a href="https://www.rabbitmq.com/community-plugins.html">官网</a>，下载
     * rabbitmq_delayed_message_exchange 插件
     */
    @Bean
    public CustomExchange delayedExchange() {
        Map<String, Object> args = new HashMap<>();
        // 自定义交换机的类型
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MqCons.DELAYED_EVENT_EXCHANGE, "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue payOrderDelayedQueue() {
        Map<String, Object> arguments = new HashMap<>();
        return new Queue(PayOrderCloseMQ.DELAY_QUEUE, true, false, false, arguments);
    }

    @Bean
    public Binding orderDelayedBinding(@Qualifier("payOrderDelayedQueue") Queue queue,
                                       @Qualifier("delayedExchange") CustomExchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with(PayOrderCloseMQ.DELAY_ROUTING_KEY).noargs();
    }


    /**
     * 支付商户通知-延时队列
     */
    @Bean
    public Queue payOrderMchNotifyDelayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        return new Queue(MchNotifyMQ.DELAY_QUEUE, true, false, false, arguments);
    }

    @Bean
    public Binding payOrderMchNotifyDelayBinding(@Qualifier("payOrderMchNotifyDelayQueue") Queue queue,
                                       @Qualifier("delayedExchange") CustomExchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with(MchNotifyMQ.ROUTE_KEY).noargs();
    }

    /**
     * 退款
     */
    @Bean
    public Queue refundOrderDelayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        return new Queue(RefundOrderMQ.DELAY_QUEUE, true, false, false, arguments);
    }

    @Bean
    public Binding refundOrderDelayBinding(@Qualifier("refundOrderDelayQueue") Queue queue,
                                                 @Qualifier("delayedExchange") CustomExchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with(RefundOrderMQ.DELAY_ROUTING_KEY).noargs();
    }
}
